﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using Models;
using Infrastructures;
using System.Web.Security;
using System.Web.Http.Controllers;
using System.Reflection;
using ConcreteService;

namespace SponsorshipWeb
{
    public class CommonAuthorize : AuthorizeAttribute
    {
        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            if (HttpContext.Current.Session["Staff"] == null)
                return false;

            var info = HttpContext.Current.Session["Staff"] as LoginInfo;
            var attr = actionContext.ActionDescriptor.GetCustomAttributes<UserRoleAttribute>();
            if (attr.Count == 0)
                return true;

            var item = attr.FirstOrDefault(x => x.Role.Any(y => y == info.UserType));
            if (item == null)
                return false;

            return true;
        }
    }

    public class UserRoleAttribute : Attribute
    {
        internal APPRole[] Role { get; set; }

        public UserRoleAttribute(params APPRole[] role)
        {
            this.Role = role;
        }
    }

    public class AuthUtil
    {
        public static LoginInfo Authorize(UserInfo obj, int companyID = 0, int studentID = 0)
        {
            UserInfoService userService = ServiceFactory.CreateInstance<UserInfoService>();
            //UserInfo userinfo = userService.Get<UserInfo>(obj.UserNo.ToString());
            LoginInfo info = new LoginInfo();
            info.LoginTime = DateTime.Now;
            info.UpdatedTime = DateTime.Now;
            info.UserID = obj.UserNo;
            info.UserType = obj.Role;
            info.Logo = SystemConfig.Get("UploadHttpPath") + obj.HeadImage;
            //info.CompanyID = companyID;
            //info.StudentID = studentID;

            HttpContext.Current.Session["Staff"] = info;
            FormsAuthentication.SetAuthCookie(info.UserID.ToString(), false);

            return info;

            //CacheManager.Put(info);
        }

        public static void UpdateAuthentication(LoginInfo obj)
        {
            HttpContext.Current.Session["Staff"] = obj;
        }

        public static void Unauthorize()
        {
            HttpContext.Current.Session["Staff"] = null;

            //var obj = GetAuthentication(code);
            //if (obj == null)
            //    return;

            //CacheManager.Delete<LoginInfo>(code);
        }

        public static LoginInfo GetAuthentication()
        {
            if (HttpContext.Current.Session == null)
                return null;

            return HttpContext.Current.Session["Staff"] as LoginInfo;
        }
    }

    public class AssocType
    {
        private static List<AssocType> lstAssocType;

        public string NatureNo { get; set; }
        public Dictionary<string, string> DictType;
        //public string TypeNo { get; set; }
        //public string TypeName { get; set; }

        static AssocType()
        {
            lstAssocType = new List<AssocType>();
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("900000048", "组织类-学生会");
            dic.Add("900000049", "组织类-团委");
            dic.Add("900000050", "组织类-社团联合会");
            dic.Add("900000051", "组织类-其他");

            Dictionary<string, string> dicAssoc = new Dictionary<string, string>();
            dicAssoc.Add("900000052", "社团类-青年志愿者服务队");
            dicAssoc.Add("900000053", "社团类-红十字会");
            dicAssoc.Add("900000054", "社团类-体育类社团");
            dicAssoc.Add("900000055", "社团类-棋牌类社团");
            dicAssoc.Add("900000056", "社团类-动漫社");
            dicAssoc.Add("900000057", "社团类-文娱类社团");
            dicAssoc.Add("900000058", "社团类-兴趣类社团");
            dicAssoc.Add("900000059", "社团类-其他社团");

            AssocType type = new AssocType();
            type.NatureNo = "900000112";
            type.DictType = dic;

            AssocType typeAssoc = new AssocType();
            typeAssoc.NatureNo = "900000113";
            typeAssoc.DictType = dicAssoc;

            lstAssocType.Add(type);
            lstAssocType.Add(typeAssoc);
        }

        public static Dictionary<string, string> ListAssocType(string id)
        {
            return lstAssocType.FirstOrDefault(x => x.NatureNo == id).DictType;
        }
    }
}